package com.spring.security.validate.code.sms;

import com.spring.security.properties.SecurityProperties;
import com.spring.security.validate.code.ValidateCode;
import com.spring.security.validate.code.ValidateCodeGenerator;
import com.spring.security.validate.code.image.ImageCode;
import lombok.Data;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

/**
 * 短信验证码生成器
 *
 * @author hekang
 * @date 2020/01/14
 */
@Data
@Component("smsValidateCodeGenerator")
public class SmsCodeGenerator implements ValidateCodeGenerator {

    @Autowired
    private SecurityProperties securityProperties;

    /**
     * 创建图片的代码
     *
     * @param request
     * @return {@link ImageCode}
     */
    @Override
    public ValidateCode createCode(ServletWebRequest request) {
        //生成随机码
        String code = RandomStringUtils.randomNumeric(securityProperties.getCode().getSms().getLength());

        return new ValidateCode(code, securityProperties.getCode().getSms().getExpireIn());
    }

}